﻿using System.ComponentModel;
using System.Windows.Forms;
using PolePosition.v3.EntityLibrary;
using System.Linq;

namespace PolePosition.v3.Desktop.Editors.Controls
{
    public partial class Transformer : DialogEditBase
    {
        public Transformer()
        {
            InitializeComponent();

            BindCombos();
        }

        public Common.Enumerations.DataTypes DataType { get; private set; }
        private ProjectCommon2.Generics.EntityCollection<VendorDistributionNotesXTransformerEntity> m_NotexXTransfomer;

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data
        {
            get
            {
                return bindingSourceTransformer.DataSource;
            }
            set
            {
                bindingSourceTransformer.SuspendBinding();
                SetupDatabindings(value);
                bindingSourceTransformer.DataSource = value;
                bindingSourceTransformer.ResumeBinding();
            }
        }

        private void BindCombos()
        {
            if (Program.LookupTableData != null)
            {
                ComboOptions.DistributionLookUp(comboBoxSpec, VendorDistributionLookupsType.TransformerType);
                ComboOptions.DistributionLookUp(comboBoxClass, VendorDistributionLookupsType.Class);
                ComboOptions.DistributionLookUp(comboBoxPhase, VendorDistributionLookupsType.Phase);
                ComboOptions.DistributionLookUp(comboBoxType, VendorDistributionLookupsType.Type);
                ComboOptions.DistributionLookUp(comboBoxArrestorKv, VendorDistributionLookupsType.ArrestorKv);
                ComboOptions.DistributionLookUp(comboBoxCan, VendorDistributionLookupsType.CutoutKv);
                ComboOptions.DistributionLookUp(comboBoxPCBType, VendorDistributionLookupsType.PCBType);
                ComboOptions.DistributionLookUp(comboBoxCan, VendorDistributionLookupsType.Can);
                ComboOptions.DistributionLookUp(comboBoxDeterminedBy, VendorDistributionLookupsType.DeterminedBy);

                listSelectorNotes.DisplayMember = VendorDistributionLookupsEnumerations.Columns.LookUpText.ToString();
                listSelectorNotes.ValueMember = VendorDistributionLookupsEnumerations.Columns.Vendor_Distribution_LookupId.ToString();
                if (EditAllowed)
                    listSelectorNotes.DataSource = Program.LookupTableData.VendorDistributionLookups.Where(x => x.LookUpType == (int)VendorDistributionLookupsType.Keywords).ToList();
                else
                    listSelectorNotes.Clear();
            }
        }

        [DefaultValue(false)]
        private bool m_EditAllowed;
        public bool EditAllowed
        {
            get
            {
                return m_EditAllowed;
            }
            set
            {
                m_EditAllowed = value;
            }
        }

        private void SetupDatabindings(object data)
        {
            if (data.GetType() == typeof(VendorDistributionTransformerEntity))
            {
                DataType = Common.Enumerations.DataTypes.ProjectsDistributionInspection;

                bindingSourceTransformer.DataSource = typeof(VendorDistributionTransformerEntity);

                textBoxSerialNumber.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "SerialNumber", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxComments.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Comments", true, DataSourceUpdateMode.OnPropertyChanged));
                LabelCreatedDate.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "CreatedDate", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxLatitude.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Latitude", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxLongitude.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Longitude", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxManufacturer.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Manufacturer", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxPrimary.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Primary", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxSecondary.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Secondary", true, DataSourceUpdateMode.OnPropertyChanged));
                textBoxPPM.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "PPM", true, DataSourceUpdateMode.OnPropertyChanged));
                txtImpeddance.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Impeddance", true, DataSourceUpdateMode.OnPropertyChanged));
                txtTaps.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Taps", true, DataSourceUpdateMode.OnPropertyChanged));
                txtPolarity.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "Polarity", true, DataSourceUpdateMode.OnPropertyChanged));
                txtManufDate.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "ManufDateFormatted", true, DataSourceUpdateMode.OnPropertyChanged));
                txtTestNo.DataBindings.Add(new Binding("Text", bindingSourceTransformer, "TestNumber", true, DataSourceUpdateMode.OnPropertyChanged));

                comboBoxSpec.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "TransformerTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxClass.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "ClassFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxPhase.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "PhaseFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxType.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "TypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxArrestorKv.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "ArrestorKv", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCutOut.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "CutoutKv", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxPCBType.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "PCBTypeFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxCan.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "CanFk", true, DataSourceUpdateMode.OnPropertyChanged));
                comboBoxDeterminedBy.DataBindings.Add(new Binding("SelectedValue", bindingSourceTransformer, "DeterminedBy", true, DataSourceUpdateMode.OnPropertyChanged));

                checkBoxArrestor.DataBindings.Add(new Binding("Checked", bindingSourceTransformer, "Arrestor", true, DataSourceUpdateMode.OnPropertyChanged));
                checkBoxDualRated.DataBindings.Add(new Binding("Checked", bindingSourceTransformer, "DualRated", true, DataSourceUpdateMode.OnPropertyChanged));
                checkBoxCutout.DataBindings.Add(new Binding("Checked", bindingSourceTransformer, "Cutout", true, DataSourceUpdateMode.OnPropertyChanged));
                checkBoxPre1978.DataBindings.Add(new Binding("Checked", bindingSourceTransformer, "Pre1978", true, DataSourceUpdateMode.OnPropertyChanged));
                chkSticker.DataBindings.Add(new Binding("Checked", bindingSourceTransformer, "Sticker", true, DataSourceUpdateMode.OnPropertyChanged));

                LoadDataAsynch((data as PolePosition.v3.EntityLibrary.VendorDistributionTransformerEntity).Vendor_Distribution_Transformers_TransformerId.Value);
            }
        }

        private void LoadDataAsynch(int transformerId)
        {
            listSelectorNotes.LoadingVisible = true;
            backgroundWorkerNotes.RunWorkerAsync(transformerId);
        }

        private void backgroundWorkerNotes_DoWork(object sender, DoWorkEventArgs e)
        {
            m_NotexXTransfomer = ServiceProxy.UtilityService.GetDistributionInspectionProjectNotesForTransformer((int)e.Argument);

            if (backgroundWorkerNotes.CancellationPending)
                e.Cancel = true;
        }

        private void backgroundWorkerNotes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                return;

            listSelectorNotes.LoadingVisible = false;

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("A Data Retrieval Error has occurred attempting to load the top types. Please close editor and try again.", "Data Retrieval Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                listSelectorNotes.Enabled = false;
                return;
            }

            if (DataType == Common.Enumerations.DataTypes.ProjectsDistributionInspection)
            {
                if (m_NotexXTransfomer != null)
                {
                    if (EditAllowed)
                    {
                        var _NotesValues = from note in m_NotexXTransfomer
                                         select note.Vendor_Distribution_NoteFk.Value;
                        listSelectorNotes.SetSelectedValues<int>(_NotesValues.ToList<int>());
                    }
                    else
                    {
                        if (m_NotexXTransfomer.Count() > 0)
                        {

                            //this will only show selected items
                            var _NotesValues = from note in m_NotexXTransfomer
                                             join lookupdata in Program.LookupTableData.VendorDistributionLookups
                                             on note.Vendor_Distribution_NoteFk equals lookupdata.Vendor_Distribution_LookupId
                                             select lookupdata;

                            listSelectorNotes.ReadOnlyNoneSelectedVisible = false;
                            listSelectorNotes.DataSource = _NotesValues.ToList();
                        }
                        else
                        {
                            listSelectorNotes.ReadOnlyNoneSelectedVisible = true;
                        }
                    }
                }

                listSelectorNotes.Enabled = true;
            }
        }


    }
}
